Repository navigation
Keep carriage returns and shared-underscore _xHHHH_ text on export - #1084
Open
vtxe02023274 wants to merge 1 commit into
Open
vtxe02023274 wants to merge 1 commit into
vtxe02023274 wants to merge 1 commit into
Conversation
EncodeString wrote \r as a raw character, and an XML parser reads a raw CR or CRLF as LF, so the CR was gone when the file was opened in Excel or read back with Query. Excel itself writes it as _x000D_, which is what the encoder now does. The _xHHHH_ escape consumed the trailing underscore, so in _x0041_x0042_ the second sequence was left unescaped and Excel showed _x0041B. The regex now matches only the leading underscore, giving _x005F_x0041_x005F_x0042_ like Excel.
Contributor
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two kinds of text change on export, both in
XmlHelper.EncodeString.Carriage return.
\ris written as a raw character. An XML parser reads a raw CR or CRLF as LF, so"a\r\nb"comes back fromQueryas"a\nb", and Excel shows the same. Excel writes CR as_x000D_(a cell with CRLF saved by Excel hasa_x000D_followed by the line break insharedStrings.xml), andDecodeStringalready reads that back as\r. The encoder now writes_x000D_too._xHHHH_sequences that share an underscore. The escape regex_(x[\dA-Fa-f]{4})_consumes the trailing underscore, so in_x0041_x0042_only the first sequence is escaped. The cell is written as_x005F_x0041_x0042_, and both Excel andQueryshow_x0041B. The regex now matches only the leading underscore (_(?=x[\dA-Fa-f]{4}_)), giving_x005F_x0041_x005F_x0042_, which is what Excel writes for that text. A single_x0041_is encoded as before.Checked with Excel 365: I exported 28 strings (control characters, surrogates,
_xHHHH_variants, CR/LF, XML special characters) and read the cells through COM. On master two differ from the input (_x0041_x0042_shows as_x0041B,cr\r\nlfloses the CR); with this change all 28 match.Tests:
TestExportCarriageReturnAndEscapedUnderscores(inline and shared strings): checks the written XML and thatQueryreturns the exported textIssue149(sync and async) no longer skips\rin the export round tripsTestIssue294NewLineexpected the raw CR in the XML; it now expects_x000D_On master 10 of these fail; with the change
tests/MiniExcel.OpenXml.Tests(584) andtests/MiniExcel.Csv.Tests(92) pass on net8.0, net9.0, net10.0 and net11.0, andsrc/MiniExcel.OpenXmlbuilds for netstandard2.0.Summary by CodeRabbit